from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField, SelectField
from wtforms.validators import DataRequired, Length, ValidationError
from application.users.models import Users


# 注册表单
class RegisterForm(FlaskForm):
    username = StringField('用户名',
                           validators=[DataRequired(message='用户名必填'), Length(max=128, message="用户名长度不能超过128位")])
    password = PasswordField('密码',
                             validators=[DataRequired(message='密码必填'), Length(min=6, message="密码不能少于6位")])
    # DataRequired()必填
    fullname = StringField('姓名',
                             validators=[DataRequired(message='姓名必填'), Length(max=128, message="姓名长度不能超过128位")])
    submit = SubmitField('提交')

    # 验证用户名是否存在
    def validate_username(self, field):

        username = field.data
        if Users.query.filter_by(username=username).first():
            raise ValidationError('用户名已存在')


# 登录表单
class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])

    remember_me = BooleanField('记住我')

    submit = SubmitField('提交')

